Core Tutorial
This tutorial shows you how to interact with the core.
Usage
The core variable injected into API signatures contains a reference to the Core instance.
It is used to interact with service providers, read configuration(s) and other core functionality.
Common
These methods are shared between the server and client:
// Gets a configuration value
const value = core.config('resolve.by.key', 'optional default value');
// Retrieves an instance of a service
const service = core.make('namespace/service');
// Registers a new ServiceProvicer class
core.register(SomeServiceProvider, {/* registration options */});
// Registers a new singleton factory for service
core.singleton('namespace/service', () => new SomeService());
// Registers a new instance factory for service
core.instance('namespace/service', () => new SomeService());
// Checks if given service exists
const exists = core.has('namespace/service');
// Subscribe to an event
core.on('event-name', () => {});
Client
The client has some extra methods for dealing with user data, requests, resources and applications:
// Creates a URL based on the public path
const url = core.url('/foo/bar');
// Creates a new fetch() request
const promise = core.request('http://url', {/* options */}, 'type')
// Launches an application
core.run('Preview', {file: {path: 'home://image.png'}})
// Launches a new application based on a file
core.open({path: 'home://image.png', mime: 'image/png'});
// Gets user data
const user = core.getUser();
// Send an event to the server
core.send('event-name', 1, 2, 3);
Global 'OSjs' namespace
The window global OSjs also lets you reach some of the core functionality.
// Same as above (but some services are restricted)
OSjs.make();
// These are the same as above
OSjs.open();
OSjs.request();
OSjs.run();
OSjs.url();
Client Events
init => ()- Main initosjs/core:boot => ()- Core boot (before providers have initialized)osjs/core:booted => ()- Core booted (after providers have initialized)osjs/core:start => ()- Core start (before providers have started)osjs/core:started => ()- Core started (after providers have started)osjs/core:logged-in => ()- User successfully logged inosjs/core:connect => ()- Connection establishedosjs/core:disconnect => ()- Connection lostosjs/core:destroy => ()- Core destroyosjs/application:launch => (name, args, options)- Application pre-launchosjs/application:launched => (name, app)- Application launchedosjs/application:*:launched => (app)- Application launched (*is metadata name)osjs/application:create => (app)- Application createosjs/application:destroy => (app)- Application destroyosjs/window:create => (win)- Window createosjs/window:render => (win)- Window renderosjs/window:change => (win, key, value)- Window changedosjs/window:transitionend => (ev, win)- Window transition endedosjs/desktop:transform => (rect)- Desktop transformedosjs/locale:change => (name)- Locale changedosjs/fs:mount => ()- Filesystem mountedosjs/fs:unmount => ()- Filesystem unmountedosjs/settings:save => ()- Settings savedosjs/settings:load => ()- Settings loadedosjs/vfs:* => (...args)- VFS Method call started (*is method name)osjs/vfs:*:done => (...args)- VFS Method call finished (*is method name)osjs/tray:create => (entry)- Tray entry createdosjs/tray:remove => (entry)- Tray entry removedosjs/tray:update => (entries)- Tray entry updatedosjs/notification:create => (notif)- Notification createdosjs/notification:destroy => (notif)- Notification destroyed
Client Services
These are the default provided services and their signatures:
osjs/application => (data)- Creates a new Application instanceosjs/window => (options)- Creates a new Window instanceosjs/event-handler => (name)- Creates a new EventEmitter instanceosjs/websocket => (...args)- Creates a new WebSocket instanceosjs/notification => (options?)- Creates a new Notification entryosjs/tray => (options?)- Creates a new Tray entryosjs/clipboard => ()- APIs for performing Clipboardosjs/settings => ()- APIs for Settingsosjs/vfs => ()- APIs for VFSosjs/locale => ()- APIs for handling Localizationosjs/auth => ()- APIs for Authenticationosjs/contextmenu => (options?)- APIs for Context Menusosjs/dialog => (name, ...args)- APIs for Dialogsosjs/dialogs => ()- APIs for Custom Dialogsosjs/dnd => ()- APIs for performing Drag-and-Drop operationsosjs/theme => ()- APIs for Themesosjs/packages => ()- APIs for Package Managementosjs/session => ()- APIs for Sessionosjs/desktop => ()- APIs for desktoposjs/panels => ()- APIs for panels
Example:
core.make('osjs/settings').save();
Server
The server also has some extra methods:
// Broadcast an event to all connected users (WebSocket)
core.broadcast('event-name', [1, 2, 3])
// Broadcast an event to a set of users
core.broadcast('event-name', [1, 2, 3], ws => {
//The original 'req' containing session etc
//ws.upgradeReq
//The original session data
//ws._osjs_client
return true;
});
// Broadcast to all alias but with expanded arguments:
core.broadcastAll('event-name', 1, 2 , 3);
// Broadcast to a specific user, with expanded arguments:
core.broadcastUser('username', 'event-name', 1, 2 , 3);
// Express server
const app = core.app;
// WebSocket server
const ws = core.ws;
// Session server
const session = core.session;
[info] You can listen for broadcast events in the client with
// Client-side example:
core.on('event-name', (a, b, c) => console.log(a, b, c)) // => 1 2 3
Server Services
These are the default provided services and their signatures:
osjs/express => ()- APIs for Expressosjs/packages => ()- APIs for Packagesosjs/vfs => ()- APIs for VFSosjs/fs => ()- APIs for Filesystem interaction
Events
init => ()- Main initosjs/core:start => ()- Core startosjs/core:started => ()- Core startedosjs/core:ping => (req)- User pinged the serverosjs/core:vfs:watch:change => ({mountpoint, target, type})- VFS watch triggerosjs/core:logged-in => (session)- User logged inosjs/core:logging-out => (session)- User is logging out
OS.js Web Desktop - © Anders Evenrud <andersevenrud@gmail.com>