Class: Core

Core

Main class for core plug-in.

Constructor

new Core(plugins)

Loads an array of plug-ins.
Parameters:
Name Type Description
plugins Array Plugin object instances.
Since:
  • 0.0.0
Source:
Fires:
Listens to Events:
Example
import Core from 'core'

const modernUploader = new Core([
   new SomePlugin1(),
   new SomePlugin2()
])

Extends

Members

(readonly) name :string

A name for this plug-in.
Type:
  • string
Since:
  • 0.0.0
Inherited From:
Source:

Methods

fire(event) → {Promise}

Trigger an event to be passed to all other registered event listeners. This event will be bubbled, starting with the first registered listener, and ending with the last.
Parameters:
Name Type Description
event Event event to pass to all registered listeners for this event type.
Since:
  • 0.0.0
Source:
Returns:
Once the event has been bubbled to all registered listeners, the result will be returned by resolving or rejecting this returned Promise, depending on the outcome. If the event is cancelled by a listener, this Promise will be rejected with any data provided by the cancelling listener. If this event is not cancelled, any data provided by all listeners will be included when resolving the returned Promise.
Type
Promise
Example
// A plug-in that wants to add a new item (File/Blob/etc),
// will fire an "add" event, which will be handled by the
// core plug-in, resulting in the addition of the item
// to the internal cache maintained by the core plug-in.
// But some other plug-in could also prevent this add
// from happening before it reaches the core plug-in
// (perhaps a validator plug-in, for example).
core.fire(
   new Event({
      type: 'add',
      payload: {
         item: someFile
      }
   })
)
.then(
   function added(event) {
      // the item was added
   },

   function notAdded(event) {
      // something prevented item from being added
   }
)

get(entryQuery, cloneopt) → {Object|Array|null}

This looks up items in the store. It also provides an opportunity to retrieve all items maintained by the system. Currently, items may be looked up by ID, or you may retrieve all items from the store. By default, the returned items will be deeply cloned, but this can be switched off to save processor cycles for large data sets. Any records that you would like to modify must be sent back to the system in an updateData event.
Parameters:
Name Type Attributes Default Description
entryQuery Object | undefined Retrieve the associated records from the store. An object describes the specific entries to return. If this is undefined, all records will be returned.
Properties
Name Type Description
id string | number | Array The ID or IDs of the entries to retrieve.
clone boolean <optional>
true
Since:
  • 0.0.0
Source:
Returns:
One or more matching records, or null if no matches were found. If only one record was requested, the an Array will not be returned - only the entry object or null if the entry cannot be located. If multiple records are requested, an array will always be returned with any matching entries. If no entries match, the array will be empty.
Type
Object | Array | null
Example
// get one record
const record = api.get({id: 'uuid-000'})
expect(record).toEqual(uuid000Record)

// get multiple records
const record = api.get({id: ['uuid-000', 'uuid-001']})
expect(record).toEqual([uuid000Record, uuid001Record])

// get all records
const record = api.get()
expect(record).toEqual([uuid000Record, uuid001Record, uuid002Record, ...])

(abstract) load(api) → {Promise|undefined}

Called to load this specific plug-in. All setup logic, async or sync, should be completed as part of handling this call. For example, register event handlers here. Do NOT mutate the passed API object directly. Only return a new object that adds new methods or augments existing ones.
Parameters:
Name Type Description
api Core API for core plug-in.
Since:
  • 0.0.0
Inherited From:
Source:
Throws:
If there is an immediate issue loading the plug-in.
Type
Error
Returns:
If a Promise is returned, resolve or reject once the outcome is known. Otherwise, return nothing for success or throw for failure. To override existing API methods, or provide new API methods, simply return an object (or pass one through your call to resolve the returned Promise). Any new or overridden methods will be available to all other plug-ins.
Type
Promise | undefined
Example
// simple load implementation
load(api) {
   api.on('add', event => {
      // handle "add" event
   })
}

// adding a new API method
load(api) {
   return {
      sayHi: () => 'hi'
   }
}

// overriding an existing API method
load(api) {
   // Always do this to ensure the original
   // method that you are overriding maintains
   // access to its expected context. Only
   // needed if you intend to call the original
   // method in the new method.
   const oldSayHi = api.add.bind(api)

   return {
      sayHi: () => {
         return oldSayHi() + ' you!'
      }
   }
}

on(typeOrListenersObject, listener)

Register one or more handlers for one or more specific events.
Parameters:
Name Type Description
typeOrListenersObject string | Object Register one listener by specifying the event type as a string here, followed by the listener function as the next parameter, or pass a single object parameter with event type properties and listener function values to conveniently register for multiple events.
listener eventCallback Listener Function to be called when the passed event type is fired. This parameter is ignored if the first parameter is a listener object. In that case, it must be supplied as the value for each event type property in the passed object.
Since:
  • 0.0.0
Source:
Example
// register a single listener for an "add" event
core.on('add', event => {
   // handle "add" event
})

// register a listener for the "add" event,
// and another for the "remove" event
core.on({
   add: event => {
      // handle "add" event
   },
   remove: event => {
      // handle "remove" event
   }
})

onAll(listener)

Register a listener for all events. This may be useful for plug-ins that need to be notified whenever any event is triggered in the system. A good example would be a plug-in that logs all activity.
Parameters:
Name Type Description
listener eventCallback Listener function
Since:
  • 0.0.0
Source:
Example
// register for all system events
core.onAll(event => {
   console.log(`I just received an event of type ${event.type}.`)
   // do something else with this event:
   //   - return a new value for the originator
   //   - cancel the event
   //   - passively listen or react in some other way
})