├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.ts ├── package.json ├── src ├── data-binding.ts ├── web-socket-data-binding-callback.ts └── web-socket-data-binding.service.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Node 2 | node_modules/* 3 | npm-debug.log 4 | 5 | # TypeScript 6 | *.js 7 | *.map 8 | *.d.ts 9 | 10 | # JetBrains 11 | .idea 12 | .project 13 | .settings 14 | .idea/* 15 | *.iml 16 | 17 | # Windows 18 | Thumbs.db 19 | Desktop.ini 20 | 21 | # Mac 22 | .DS_Store 23 | **/.DS_Store 24 | 25 | # Vim 26 | .*.swp 27 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Node 2 | node_modules/* 3 | npm-debug.log 4 | 5 | # DO NOT IGNORE TYPESCRIPT FILES FOR NPM 6 | # TypeScript 7 | # *.js 8 | # *.map 9 | # *.d.ts 10 | 11 | # JetBrains 12 | .idea 13 | .project 14 | .settings 15 | .idea/* 16 | *.iml 17 | 18 | # Windows 19 | Thumbs.db 20 | Desktop.ini 21 | 22 | # Mac 23 | .DS_Store 24 | **/.DS_Store 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Fabian Schaffert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ng2-django-channels-data-binding 2 | 3 | ## Installation 4 | 5 | To install this library, run: 6 | 7 | ```bash 8 | $ npm install ng2-django-channels-data-binding ng2-django-channels-demultiplexing --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | Sample Angular `AppModule`: 14 | 15 | ```typescript 16 | import { BrowserModule } from '@angular/platform-browser'; 17 | import { NgModule } from '@angular/core'; 18 | 19 | import { AppComponent } from './app.component'; 20 | 21 | // Import libraries 22 | import { Ng2DjangoChannelsDataBindingModule } from 'ng2-django-channels-data-binding'; 23 | import { Ng2DjangoChannelsDemultiplexingModule } from 'ng2-django-channels-demultiplexing'; 24 | 25 | @NgModule({ 26 | declarations: [ 27 | AppComponent 28 | ], 29 | imports: [ 30 | BrowserModule, 31 | 32 | // Specify library as an import and configure the WebSocket URL 33 | Ng2DjangoChannelsDataBindingModule, 34 | Ng2DjangoChannelsDemultiplexingModule.forRoot({websocket_url: 'ws://127.0.0.1:8001/api/ws'}) 35 | ], 36 | providers: [], 37 | bootstrap: [AppComponent] 38 | }) 39 | export class AppModule { } 40 | ``` 41 | 42 | Once imported, you can use WebSocketDataBindingService in your Angular application: 43 | 44 | ```typescript 45 | import { Injectable } from '@angular/core'; 46 | import { DataBinding, WebSocketDataBindingService } from 'ng2-django-channels-data-binding'; 47 | 48 | @Injectable() 49 | export class SomeService { 50 | constructor(private webSocketDataBindingService: WebSocketDataBindingService) { 51 | webSocketDataBindingService.subscribe( 52 | // Django Channels Demultiplexer Stream 53 | 'hero', 54 | // Django model 55 | 'hero_service.hero', 56 | (payload: DataBinding) => { 57 | if(payload.action === 'create') { 58 | ( ... ) 59 | } else if(payload.action === 'update') { 60 | ( ... ) 61 | } else if(payload.action === 'delete') { 62 | ( ... ) 63 | } 64 | } 65 | ); 66 | } 67 | 68 | private create(hero: Hero): void { 69 | this.webSocketDataBindingService.create( 70 | 'hero', 'hero_service.hero', {'name': hero.name}); 71 | } 72 | 73 | private update(hero: Hero): void { 74 | this.webSocketDataBindingService.update( 75 | 'hero', 'hero_service.hero', hero.id, {'name': hero.name}); 76 | } 77 | 78 | delete(hero: Hero): void { 79 | this.webSocketDataBindingService.delete( 80 | 'hero', 'hero_service.hero', hero.id); 81 | } 82 | } 83 | ``` 84 | 85 | ## License 86 | 87 | MIT © [Fabian Schaffert](mailto:fabian@schaffert.cc) 88 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import { NgModule, ModuleWithProviders } from '@angular/core'; 2 | 3 | import { WebSocketDataBindingService } from './src/web-socket-data-binding.service'; 4 | 5 | export { DataBinding } from './src/data-binding'; 6 | export { WebSocketDataBindingService } from './src/web-socket-data-binding.service'; 7 | 8 | @NgModule({ 9 | providers: [ 10 | WebSocketDataBindingService 11 | ] 12 | }) 13 | export class Ng2DjangoChannelsDataBindingModule { } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng2-django-channels-data-binding", 3 | "version": "0.1.0", 4 | "description": "Angular 2 library for interacting with a Django Channels data binding backend.", 5 | "main": "index.js", 6 | "scripts": { 7 | "prepublish": "tsc", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/devmapal/ng2-django-channels-data-binding.git" 13 | }, 14 | "keywords": [ 15 | "angular", 16 | "angular2", 17 | "django", 18 | "channels", 19 | "websocket", 20 | "data binding" 21 | ], 22 | "typings": "./index.d.ts", 23 | "author": "Fabian Schaffert", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/devmapal/ng2-django-channels-data-binding/issues" 27 | }, 28 | "homepage": "https://github.com/devmapal/ng2-django-channels-data-binding#readme", 29 | "peerDependencies": { 30 | "@angular/core": "2.x", 31 | "ng2-django-channels-demultiplexing": "0.1.0" 32 | }, 33 | "devDependencies": { 34 | "@angular/core": "2.x", 35 | "@types/core-js": "^0.9.35", 36 | "ng2-django-channels-demultiplexing": "0.1.0", 37 | "rxjs": "5.x", 38 | "typescript": "2.x", 39 | "zone.js": ">=0.6.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/data-binding.ts: -------------------------------------------------------------------------------- 1 | export class DataBinding { 2 | action: string; 3 | pk: any; 4 | model: string; 5 | data: any; 6 | } 7 | -------------------------------------------------------------------------------- /src/web-socket-data-binding-callback.ts: -------------------------------------------------------------------------------- 1 | import { DataBinding } from './data-binding'; 2 | 3 | export interface WebSocketDataBindingCallback { 4 | (payload: DataBinding): void; 5 | } 6 | -------------------------------------------------------------------------------- /src/web-socket-data-binding.service.ts: -------------------------------------------------------------------------------- 1 | import { EventEmitter, Injectable } from '@angular/core'; 2 | 3 | import { WebSocketDataBindingCallback } from './web-socket-data-binding-callback'; 4 | import { WebSocketDemultiplexerService } from 'ng2-django-channels-demultiplexing'; 5 | import { DataBinding } from './data-binding'; 6 | 7 | @Injectable() 8 | export class WebSocketDataBindingService { 9 | private eventByStreamModel: Map>>; 10 | 11 | constructor(private webSocketDemultiplexerService: WebSocketDemultiplexerService) { 12 | this.eventByStreamModel = new Map>>(); 13 | } 14 | 15 | private onmessage(stream: string, payload: DataBinding): void { 16 | if(!this.eventByStreamModel.has(stream)) { 17 | // No one is subscribed to this ModelStream, ignore 18 | return; 19 | } 20 | 21 | let eventByModel = this.eventByStreamModel.get(stream); 22 | if(!eventByModel.has(payload.model)) { 23 | // No one is subscribed to this Model, ignore 24 | return; 25 | } 26 | 27 | let eventEmitter = eventByModel.get(payload.model); 28 | eventEmitter.emit(payload); 29 | } 30 | 31 | create(stream: string, model: string, data: Object): void { 32 | this.webSocketDemultiplexerService.sendData( 33 | stream, 34 | { 35 | 'action': 'create', 36 | 'model': model, 37 | 'data': data, 38 | } 39 | ); 40 | } 41 | 42 | update(stream: string, model: string, pk: any, data: Object): void { 43 | this.webSocketDemultiplexerService.sendData( 44 | stream, 45 | { 46 | 'action': 'update', 47 | 'model': model, 48 | 'pk': pk, 49 | 'data': data, 50 | } 51 | ); 52 | } 53 | 54 | delete(stream: string, model: string, pk: any): void { 55 | this.webSocketDemultiplexerService.sendData( 56 | stream, 57 | { 58 | 'action': 'delete', 59 | 'model': model, 60 | 'pk': pk, 61 | } 62 | ); 63 | } 64 | 65 | subscribe(stream: string, model: string, callback: WebSocketDataBindingCallback): void { 66 | if(!this.eventByStreamModel.has(stream)) { 67 | this.eventByStreamModel.set(stream, new Map>()); 68 | } 69 | 70 | let eventByModel = this.eventByStreamModel.get(stream); 71 | if(!eventByModel.has(model)) { 72 | eventByModel.set(model, new EventEmitter()); 73 | } 74 | 75 | let eventEmitter = eventByModel.get(model); 76 | eventEmitter.subscribe(callback); 77 | 78 | this.webSocketDemultiplexerService.subscribe(stream, (payload: DataBinding) => { 79 | this.onmessage(stream, payload); 80 | }); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "noImplicitAny": true, 4 | "module": "commonjs", 5 | "target": "ES5", 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "sourceMap": true, 9 | "declaration": true 10 | }, 11 | "files": [ 12 | "index.ts" 13 | ], 14 | "exclude": [ 15 | "node_modules" 16 | ] 17 | } 18 | --------------------------------------------------------------------------------