Last active
May 30, 2019 21:47
-
-
Save tchak/9dfa1e8b17946e985aaec1480dddde5e to your computer and use it in GitHub Desktop.
Orbit ActionCableSource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Orbit, { | |
Source, | |
SourceSettings, | |
RecordOperation, | |
buildTransform | |
} from '@orbit/data'; | |
import { | |
JSONAPISerializer, | |
JSONAPISerializerSettings, | |
ResourceOperationsDocument | |
} from '@orbit/jsonapi'; | |
import ActionCable from '@rails/actioncable'; | |
export interface ActionCableSourceSettings extends SourceSettings { | |
cable: ActionCable.Consumer, | |
SerializerClass?: new ( | |
settings: JSONAPISerializerSettings | |
) => JSONAPISerializer; | |
} | |
export default class ActionCableSource extends Source { | |
constructor(settings: ActionCableSourceSettings) { | |
super(settings); | |
const SerializerClass = settings.SerializerClass || JSONAPISerializer; | |
const serializer = new SerializerClass({ | |
schema: settings.schema, | |
keyMap: settings.keyMap | |
}); | |
settings.cable.subscriptions.create('Operations', { | |
received: (document) => { | |
const operations = serializer.deserializeOperationsDocument(document); | |
this.applyOperations(operations); | |
} | |
}); | |
} | |
protected async applyOperations( | |
operations: RecordOperation[] | |
): Promise<void> { | |
const transforms = [buildTransform(operations)]; | |
await this._transformed(transforms); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment