DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.6.1
5.33.3 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
        • behaviors
          • ./base/
          • ./cache-requests/
          • ./can/constructor-hydrate/
          • ./can/map/
          • ./can/ref/
          • ./can/session/
          • ./can-local-store
          • ./can-memory-store
          • ./constructor/callbacks-once/
          • ./constructor/
          • ./constructor/store/
          • ./data/callbacks/
          • ./data/callbacks-cache/
          • ./data/combine-requests/
          • ./data/parse/
          • ./data/url/
          • ./data/worker/
            • options
              • name
              • worker
            • data methods
              • clear
              • createData
              • destroyData
              • getData
              • getListData
              • getSets
              • updateData
              • updateListData
          • ./fall-through-cache/
          • ./real-time/
        • modules
          • ./can/tag/
          • ./helpers/map-deep-merge
          • ./helpers/weak-reference-map
        • data types
          • DataInterface
          • Instance
          • InstanceInterface
          • List
          • ListData
        • deprecated
          • ./can/base-map/
          • ./can/merge/
          • ./can/super-map/
          • ./data/localstorage-cache/
          • ./data/memory-cache/
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-connect/data/worker/worker

  • Edit on GitHub

Connects a connection to another connection in a worker thread.

dataWorker( baseConnection )

If a Worker is provided, overwrites the "data interface methods" to package the arguments and send them as part of a postMessage to the Worker.

If a Worker is not provided, it is assumed "data-worker" is being added within a worker thread. It listens to messages sent to the Worker, calls the specified "data interface method" and sends a message back with the result.

Any data methods called on the window connection will wait until the worker connection has established a handshake.

Use

The best way to use data/worker is to create a connection module that works when loaded in either the window or in a Worker. This pattern tends to work even if workers are not supported.

The following creates a connection that does the work of cache-requests, data/url, and memory-cache in a worker thread.

The todo_connection module can be found here and looks like the following:

import connect from "can-connect";
import fixture from "can-fixture";

// If we are in the main thread, see if we can load this same
// connection in a worker thread.
let worker;
if ( typeof document !== "undefined" ) {
    worker = new Worker( System.stealURL + "?main=can-connect/data/worker/demo/todo_connection" );
}


// create cache connection
const cache = connect( [
    require( "can-connect/data/memory-cache/" )
], {
    name: "todos"
} );

// Create the main connection with everything you need.  If there is a worker,
// all data interface methods will be sent to the worker.
const todosConnection = connect( [
    require( "can-connect/data/url/url" ),
    require( "can-connect/cache-requests/cache-requests" ),
    require( "can-connect/data/worker/worker" ),
    require( "can-connect/constructor/constructor" ),
    require( "can-connect/constructor/store/store" )
], {
    url: "/todos",
    cacheConnection: cache,
    worker: worker,
    name: "todos"
} );


fixture.delay = 1000;
fixture( {
    "GET /todos": function( request ) {
        return { data: [
            { id: 1, name: "wash dishes" },
            { id: 2, name: "mow lawn" },
            { id: 3, name: "do laundry" }
        ] };
    }
} );

export default todosConnection;

The things to notice:

  1. A Worker should be passed as the worker option that loads a connection with the same name as the connection in the window. In thise case, the same connection module is loaded so everything works.

  2. A single Worker could load multiple connection modules and perform other behaviors.

Split Connection Logic

THe previous example used a single module that was loaded by both the window and the worker. This doesn't have to be the case. Two different modules could be used. For example, todo-window.js and todo-worker.js. Each might look like:

// todo-window.js
const workerURL = System.stealURL + "?main=app/models/todo-worker";

const todoConnection = connect( [
    require( "can-connect/data/worker/worker" ),
    require( "can-connect/constructor/constructor" ),
    require( "can-connect/constructor/store/store" )
], {
    worker: new Worker( workerURL ),
    name: "todos"
} );
// todo-worker.js
const cache = connect( [
    require( "can-connect/data/memory-cache/memory-cache" )
], {
    name: "todos-cache"
} );

const todoConnection = connect( [
    require( "can-connect/data/url/url" ),
    require( "can-connect/cache-requests/cache-requests" ),
    require( "can-connect/data/worker/worker" )
], {
    url: "/todos",
    cacheConnection: cache,
    name: "todos"
} );

However, the problem with the two-module approach is that it will not work if Workers are not supported by your browser.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 6.6.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news