8
0

modelconsumable.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. export default class ModelConsumable {
  7. constructor() {
  8. this.consumable = new Map();
  9. }
  10. add( item, type ) {
  11. if ( !this.consumable.has( item ) ) {
  12. this.consumable.set( item, new Map() );
  13. }
  14. this.consumable.get( item ).set( type, true );
  15. }
  16. consume( item, type ) {
  17. if ( this.test( item, type ) ) {
  18. this.consumable.get( item ).set( type, false );
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. test( item, type ) {
  25. const itemConsumables = this.consumable.get( item );
  26. if ( itemConsumables === undefined ) {
  27. return null;
  28. }
  29. const value = itemConsumables.get( type, true );
  30. if ( value === undefined ) {
  31. return null;
  32. }
  33. return value;
  34. }
  35. revert( item, type ) {
  36. const test = this.test( item, type );
  37. if ( test === false ) {
  38. this.consumable.get( item ).set( type, true );
  39. return true;
  40. } else if ( test === true ) {
  41. return false;
  42. }
  43. return null;
  44. }
  45. }